﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Asah.SiteWorx.ProductCatalog;
using System.Configuration;

/// <summary>
/// This page displays the details of products when a product is
/// selected.
/// </summary>
public partial class ProductDetail : BasePage
{
    /// <summary>
    /// This property stores the product's id given by the
    /// querystring.
    /// </summary>
    protected int MyProductId
    {
        get
        {
            return int.Parse(Request.QueryString["id"]);
        }
    }

    /// <summary>
    /// This property stores the product's size given by the
    /// querystring.
    /// </summary>
    protected int DetailSize
    {
        get
        {
            return int.Parse(ConfigurationManager.AppSettings["ProductCatalogDetailDefaultSize"].ToString());
        }
    }

    /// <summary>
    /// This property stores the return url and stores it in 
    /// viewstate.
    /// </summary>
    protected string MyReturnUrl
    {
        get
        {
            return ViewState["MyReturnUrl"].ToString();
        }
        set
        {
            ViewState["MyReturnUrl"] = value;
        }
    }

    /// <summary>
    /// This property retrieves the product object.
    /// </summary>
    protected ProductInfo MyProduct
    {
        get
        {
            return ProductList.GetProductInfo(MyProductId);
        }
    }

    /// <summary>
    /// When the page initially loads we need to set the
    /// return url to the url referrer.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            if (Page.Request.UrlReferrer == null)
                MyReturnUrl = "";
            else
                MyReturnUrl = Page.Request.UrlReferrer.PathAndQuery;
    }

    /// <summary>
    /// Redirect the page to the return url when the return
    /// linkbutton is clicked.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lnkReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect(MyReturnUrl);
    }

    /// <summary>
    /// This is what is fired when the datasource loads.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void cdsProductInfo_SelectObject(object sender, Csla.Web.SelectObjectArgs e)
    {
        e.BusinessObject = MyProduct;
    }

   
}
